// import { el } from "element-plus/lib/locale";
import { createWebHistory, createRouter, createWebHashHistory } from "vue-router";
import { ElMessage } from "element-plus";

const routes = [
    {
        path: '/',
        redirect: '/homes/datas_tabs',
    },

    {
        path: '/index',
        name: 'index',
        component: () => import('../components/Index.vue'),
        meta: { title: "协途出行" },
        children: [
            {
                path: '/homes',
                name: 'homes',
                component: () => import('../components/page_components/Home.vue'),
                meta: { title: "首页" },
                children: [
                    {
                        path: 'datas_tabs',
                        name: 'datas_tabs',
                        component: () => import('../components/page_components/Datas_tabs.vue')
                    },
                    {
                        path: 'flightDynamics',
                        name: 'flightDynamics',
                        component: () => import('../components/page_components/FlightDynamics.vue')
                    },
                    {
                        path: 'multipass',
                        name: 'multipass',
                        component: () => import('../components/query/Multipass.vue')
                    },
                ]
            },
            {
                path: 'ticketinformation',
                name: 'ticketinformation',
                component: () => import('../components/cps/TicketInformation.vue'),
                children: [
                    {
                        path: 'oneway',
                        name: 'oneway',
                        component: () => import('../views/ticket/Oneway.vue'),
                        meta: { title: "查询结果" },
                    },
                    {
                        path: 'round',
                        name: 'round',
                        component: () => import('../views/ticket/RoundTicket.vue'),
                        meta: { title: "查询结果" },
                    },
                    {
                        path: 'multipass',
                        component: () => import('../views/ticket/Multipass.vue'),
                        meta: { title: "查询结果" },
                    },
                ]
            },
            {
                path: 'flightdynamic',
                name: 'flightdynamic',
                component: () => import('../components/cps/FlightDynamic.vue'),
                meta: { title: "查询结果" },
                children: [
                    {
                        path: 'content',
                        name: 'content',
                        component: () => import('../components/cps/FlightNumber/Content.vue'),
                        meta: { title: "查询结果" },
                    },
                    {
                        path: 'list',
                        name: 'list',
                        component: () => import('../components/cps/FlightNumber/List.vue'),
                        meta: { title: "查询结果" },
                    },
                ]
            },
            {
                path: 'userinfo',
                name: 'userinfo',
                component: () => import('../components/cps/Userinfo.vue'),
                meta: { title: "个人信息" },
                beforeEnter(to, from, next) {
                    if (sessionStorage.getItem("username") != null) {
                        next()
                    } else {
                        ElMessage.warning("请先登录")
                        next({
                            path: "/LoginRegistration",
                            query: {
                                second: "first"
                            }
                        })
                    }
                },
                children: [
                    {
                        path: 'basicInformation',
                        name: 'basicInformation',
                        component: () => import('../components/user/BasicInformation.vue'),
                        meta: { title: '绑定人员' },
                        beforeEnter(to, from, next) {
                            if (sessionStorage.getItem("username")) {
                                next()
                            } else {
                                ElMessage.warning("请先登录")
                                next({
                                    path: "/LoginRegistration",
                                    query: {
                                        second: "first"
                                    }
                                })
                            }
                        }
                    },
                    {
                        path: 'myorders',
                        name: 'myorders',
                        component: () => import('../components/user/MyOrders.vue'),
                        meta: { title: "我的订单" },
                        beforeEnter(to, from, next) {
                            if (sessionStorage.getItem("username")) {
                                next()
                            } else {
                                ElMessage.warning("请先登录")
                                next({
                                    path: "/LoginRegistration",
                                    query: {
                                        second: "first"
                                    }
                                })
                            }
                        }
                    }
                ]
            },
            {
                path: 'passenger',
                name: 'passenger',
                component: () => import('../components/passenger/passenger.vue'),
                meta: { title: "机票订购" },
                children: [
                    {
                        path: 'information',
                        name: 'information',
                        component: () => import('../components/passenger/information.vue'),
                        beforeEnter(to, from, next) {
                            if (sessionStorage.getItem("username")) {
                                next()
                            } else {
                                ElMessage.warning("请先登录")
                                next({
                                    path: "/LoginRegistration",
                                    query: {
                                        second: "first"
                                    }
                                })
                            }
                        }
                    },
                    {
                        path: 'pay',
                        name: 'pay',
                        component: () => import('../components/passenger/pay.vue'),
                        beforeEnter(to, from, next) {
                            if (sessionStorage.getItem("username")) {
                                next()
                            } else {
                                ElMessage.warning("请先登录")
                                next({
                                    path: "/LoginRegistration",
                                    query: {
                                        second: "first"
                                    }
                                })
                            }
                        }
                    },
                    {
                        path: 'success',
                        name: 'success',
                        component: () => import('../components/passenger/success.vue'),
                        beforeEnter(to, from, next) {
                            if (sessionStorage.getItem("username")) {
                                next()
                            } else {
                                ElMessage.warning("请先登录")
                                next({
                                    path: "/LoginRegistration",
                                    query: {
                                        second: "first"
                                    }
                                })
                            }
                        }
                    },
                ]
            },
            {
                path: 'weather',
                name: 'weather',
                component: () => import('../views/weather/weather.vue'),
                meta: { title: "天气查询" },
            }
        ]
    },
    {
        path: '/LoginRegistration',
        name: 'LoginRegistration',
        component: () => import('../components/alone/LoginRegistration.vue'),
        meta: { title: "login" },
    },
    {
        path: "/adminLogin",
        component: () => import('../views/admin/adminLogin.vue')
    },
    {
        path: '/admin',
        component: () => import('../views/admin/adminIndex.vue'),
        beforeEnter(to, from, next) {
            if (sessionStorage.getItem("token") != null) {
                next()
            } else {
                ElMessage.warning("请先登录")
                next({
                    path: "/adminLogin",
                })
            }
        },
        children: [{
            path: '/nacos',
            component: () => import('../views/admin/nacos.vue'),
            beforeEnter(to, from, next) {
                if (sessionStorage.getItem("token") != null) {
                    next()
                } else {
                    ElMessage.warning("请先登录")
                    next({
                        path: "/adminLogin",
                    })
                }
            },
        },
        ]
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});
router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title
    }
    next()
})

export default router